// AddPingLunServlet.java - 添加评论
package com.example.li_han.servlet;

import com.example.li_han.pojo.PingLun;
import com.example.li_han.service.PingLunService;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedReader;
import java.io.IOException;
import java.lang.reflect.Type;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@WebServlet(name = "AddPingLunServlet", value = "/AddPingLunServlet")
public class AddPingLunServlet extends HttpServlet {
    private Gson gson = new Gson();

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        req.setCharacterEncoding("UTF-8");
        resp.setCharacterEncoding("UTF-8");
        resp.setContentType("application/json;charset=utf-8");

        BufferedReader br = req.getReader();
        StringBuilder sbuffer = new StringBuilder();
        String s;

        while((s = br.readLine()) != null) {
            sbuffer.append(s);
        }

        String requestBody = sbuffer.toString();
        System.out.println("收到添加评论请求: " + requestBody);

        Map<String, Object> resultMap = new HashMap<>();

        try {
            // 解析JSON数据
            Type mapType = new TypeToken<Map<String, Object>>(){}.getType();
            Map<String, Object> rawData = gson.fromJson(requestBody, mapType);

            if (rawData == null) {
                resultMap.put("code", 500);
                resultMap.put("msg", "请求数据为空");
                sendResponse(resp, resultMap);
                return;
            }

            // 验证必要字段
            if (!rawData.containsKey("userZhangHao") || !rawData.containsKey("newsId") ||
                    !rawData.containsKey("pingLunContent")) {
                resultMap.put("code", 500);
                resultMap.put("msg", "缺少必要参数");
                sendResponse(resp, resultMap);
                return;
            }

            // 创建评论对象
            PingLun pingLun = new PingLun();
            pingLun.setUserZhangHao(rawData.get("userZhangHao").toString());

            // 设置newsId
            Object newsIdObj = rawData.get("newsId");
            if (newsIdObj instanceof Double) {
                pingLun.setNewsId(((Double) newsIdObj).intValue());
            } else if (newsIdObj instanceof Integer) {
                pingLun.setNewsId((Integer) newsIdObj);
            } else {
                pingLun.setNewsId(Integer.parseInt(newsIdObj.toString()));
            }

            pingLun.setPingLunContent(rawData.get("pingLunContent").toString());
            pingLun.setPingLunTime(new Date());
            pingLun.setStatus(0); // 0-待审核

            // 添加评论
            PingLunService pingLunService = new PingLunService();
            int result = pingLunService.addPingLun(pingLun);

            if (result > 0) {
                resultMap.put("code", 200);
                resultMap.put("msg", "评论添加成功，等待审核");
                resultMap.put("pingLunId", result);
            } else {
                resultMap.put("code", 500);
                resultMap.put("msg", "评论添加失败");
            }

        } catch (Exception e) {
            e.printStackTrace();
            resultMap.put("code", 500);
            resultMap.put("msg", "服务器异常: " + e.getMessage());
        }

        sendResponse(resp, resultMap);
    }

    private void sendResponse(HttpServletResponse resp, Map<String, Object> resultMap) throws IOException {
        String json = gson.toJson(resultMap);
        resp.getWriter().write(json);
    }
}